package com.ssh;

public class Convert_6 {
	public static void main(String[] args) {
		String s = "ABCD";
		System.out.println(convert(s,3));
	}
	/**
	 * 将字符串按照Z形状排列，在内层while循环里注意i的范围
	 * @param s
	 * @param numRows
	 * @return
	 */
	public static String convert(String s, int numRows) {
        String[] sb = new String[numRows]; 
        for (int i = 0; i < numRows; i++) {
        	sb[i] = "";
        }
        int N = s.length();
        for (int i = 0,j = 0; i < N; i++,j++) {
        	j = j % numRows;
        	sb[j] += String.valueOf(s.charAt(i));
        	if (j == numRows-1) {
        		
        		while (j > 1 && i+1 < N) {
        			i++;
        			j--;
        			sb[j] += String.valueOf(s.charAt(i));
        		}
        		j = -1;
        	}
        }
        StringBuffer sb1 = new StringBuffer();
        for (int i = 0; i < numRows; i++) {
        	sb1.append(sb[i]);
        }
        return sb1.toString();
    }
}
